package com.weiqiao.report.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;

/**
 * @author 123
 * @date 2019-08-20 13:04
 */
@Slf4j
public class Tool {
    public static String getResultType(int type) {
        String result_type = "";
        switch (type) {
            case 1:
                result_type = "已结算";
                break;
            case 0:
            default:
                result_type = "未结算";
                break;
        }
        return result_type;
    }

    public static String getCustomerType(int type) {
        String customer_type = "";
        switch (type) {
            case 1:
                customer_type = "员工";
                break;
            case 3:
                customer_type = "其他";
                break;
            case 0:
            default:
                customer_type = "客户";
                break;
        }
        return customer_type;
    }

    public static String getHappenProceed(int type) {
        // 0: '佣金收入', 1: '佣金提现', 3: '订单消费',
        // 4: '充值卡充值', 5: '充值卡退款', 6: '系统增加',
        // 7: '系统减少', 8: '提现驳回'
        String happen_proceed = "";
        switch (type) {
            case 0:
                happen_proceed = "佣金收入";
                break;
            case 1:
                happen_proceed = "佣金支出";
                break;
            case 3:
                happen_proceed = "订单消费";
                break;
            case 4:
                happen_proceed = "充值卡充值";
                break;
            case 5:
                happen_proceed = "充值卡退款";
                break;
            case 6:
                happen_proceed = "系统增加";
                break;
            case 7:
                happen_proceed = "系统减少";
                break;
            case 8:
                happen_proceed = "提现驳回";
                break;
        }
        return happen_proceed;
    }

    public static void exportFinal(HttpServletResponse response, HSSFWorkbook wb) {
        // 输出Excel文件
        OutputStream output = null;
        try {
            output = response.getOutputStream();
            response.reset();
            String name = new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis());
            response.setHeader("Content-disposition", "attachment; filename=" + name + ".xls");
            response.setContentType("application/msexcel");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=utf-8");
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("connection", "close");
            wb.write(output);
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(output);
        }
    }
}
